<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
  offset="50"
  :immediate-check="false"
>
    <!-- 文章列表 -->
    <ArticleItem v-for="item in channelList" :key="item.art_id" :obj="item"></ArticleItem>
    </van-list>
  </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '@/views/Home/components/ArticleItem'
import { articlesListAPI } from '@/api'
import { timeAgo } from '@/utils/date.js'
export default {
  data () {
    return {
      channelList: [],
      loading: false,
      finished: false,
      nextTime: null,
      refreshing: false
    }
  },
  props: ['cid'],
  components: {
    ArticleItem
  },
  async created () {
    this.getArticleList()
  },
  methods: {
    async getArticleList () {
      const res = await articlesListAPI({ channel_id: this.cid, timestamp: this.nextTime })
      res.data.data.results.forEach(item => {
        item.pubdate = timeAgo(item.pubdate)
      })
      console.log(res.data.data.pre_timestamp)
      if (res.data.data.results === 0) {
        this.finished = true
      } else {
        this.nextTime = res.data.data.pre_timestamp
        this.channelList = [...this.channelList, ...res.data.data.results]
        this.loading = false // 底部加载更多状态
      }
    },
    onLoad () {
      if (this.channelList.length > 0) {
        this.getArticleList()
      }
    },
    onRefresh () {
      this.nextTime = null
      this.channelList = []
      this.getArticleList()
      this.refreshing = false
    }
  }
}
</script>
